/*
 * Copyright (c) 2010, Yawning <yawninglol at gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   * Neither the name of the Mew Developers nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package yawning.mew.sim;

import yawning.mew.sim.SimulationEventQueue.Node;

/**
 * Generic event for implementing a Discrete Event type simulator.
 *
 * @author Yawning <yawninglol at gmail.com>
 */
public abstract class SimulationEvent implements Comparable<SimulationEvent> {
	public void onTick() {
	}

	/**
	 * Event trigger hook.
	 *
	 * Note that this is called after the event is removed from the event queue.
	 */
	public void onExpire() {
	}

	protected SimulationEvent(SimulationEngine su) {
		mSimEngine = su;
		mHashCode = su.mEventHashCode++;
	}

	double mExpiryTime;
	double mNextTickTime;
	double mTickInterval;
	double mScheduleTime;
	double mUpTime;
	boolean mUp;
	int mHashCode;
	SimulationEngine mSimEngine;
	Node mQueueNode = new Node(this);	// Event queue list pointers.

	public final boolean isUp() {
		return mUp;
	}

	public final double getExpiryTime() {
		return mExpiryTime;
	}

	public final double getScheduleTime() {
		return mScheduleTime;
	}

	public final double getTickInterval() {
		return mTickInterval;
	}

	public final double getNextTickTime() {
		return mNextTickTime;
	}

	public final double getUpTime() {
		return mUp ? mUpTime + mSimEngine.getSimTime() - mScheduleTime : mUpTime;
	}

	protected final void setTickInterval(double td) {
		mTickInterval = td;
	}

	protected final void extendExpiry(double delta) {
		if (mTickInterval == 0) {
			// Needed to be able to extend non-DoTs.  Currently only used for the worst set bonus ever.
			mSimEngine.mEventQueue.remove(this);
			mNextTickTime += delta;
			mExpiryTime += delta;
			mSimEngine.mEventQueue.add(this);
		} else {
			mExpiryTime += delta;
		}
	}

	protected final void schedule(double duration, double tickDuration) {
		if (mUp) {
			mSimEngine.mEventQueue.remove(this);
			mUpTime += mSimEngine.getSimTime() - mScheduleTime;
		}
		mScheduleTime = mSimEngine.getSimTime();
		mExpiryTime = mScheduleTime + duration;
		mNextTickTime = tickDuration > 0 ? mScheduleTime + tickDuration : mExpiryTime;
		mTickInterval = tickDuration;
		mSimEngine.mEventQueue.add(this);
		mUp = true;
	}

	public final void schedule(double duration) {
		schedule(duration, 0);
	}

	public final void cancel() {
		if (mUp) {
			mUp = false;
			mSimEngine.mEventQueue.remove(this);
			mUpTime += mSimEngine.getSimTime() - mScheduleTime;
			onExpire();
		}
	}

	@Override
	public final int hashCode() {
		return mHashCode;
	}

	@Override
    public final int compareTo(SimulationEvent ev) {
        if (mNextTickTime == ev.mNextTickTime) return mHashCode - ev.mHashCode;
        else return mNextTickTime < ev.mNextTickTime ? -1 : 1;
    }
}
